package itsm.isperp.framework.web.formatter;

import itsm.isperp.framework.utils.DateUtils;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2013-8-28
 * 
 */
public class DateEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(String text) throws IllegalArgumentException {

		if (StringUtils.isEmpty(text)) {
			return;
		}
		try {
			setValue(DateUtils.parseDate(text));
		} catch (ParseException e) {
			setValue(null);
		}
	}

	@Override
	public String getAsText() throws IllegalArgumentException {
		Object obj = getValue();
		if (obj != null)
			return DateUtils.formatDate((Date) obj);
		return null;
	}
}